package com.filex.ads.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.filex.ads.RequestParams;
import com.filex.ads.banner.AdSize;
import com.filex.ads.banner.BannerAdListener;
import com.filex.ads.banner.FilexAdView;
import com.filex.ads.common.AdError;
import com.filex.ads.interstitial.FilexInterstitial;
import com.filex.ads.interstitial.InterstitialAdListener;
import com.filex.ads.nativeads.FilexNative;
import com.filex.ads.nativeads.NativeAd;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private static final String NATIVE_PLACEMENT_ID = "PD7WYW55C0005AA4";
    private static final String BANNER_PLACEMENT_ID = "PJCQGA85C13F598O";
    private static final String INTERSTITIAL_PLACEMENT_ID = "P7L7WEP5C20781BK";


    private FilexNative mFilexNative;
    private FilexAdView mFilexAdView;
    private FilexInterstitial mFilexInterstitial;

    private RelativeLayout mNativeAdContainer;
    private RelativeLayout mBannerAdContainer;
    private NativeAd mNativeAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_load_native).setOnClickListener(this);
        findViewById(R.id.btn_load_banner).setOnClickListener(this);
        findViewById(R.id.btn_load_interstitial).setOnClickListener(this);
        mNativeAdContainer = findViewById(R.id.native_ad_container);
        mBannerAdContainer = findViewById(R.id.banner_ad_container);
        loadBannerAd();
    }

    private void initBannerAd() {
        mFilexAdView = new FilexAdView(this);
        mFilexAdView.setPlacementId(BANNER_PLACEMENT_ID);
        mFilexAdView.setAdSize(AdSize.BANNER_300_250);
        // you can set auto refresh
//        mFilexAdView.setAutoRefresh(false);
//        mFilexAdView.setRefreshTimeMillis(30000); // 30s
        mFilexAdView.setBannerAdListener(new BannerAdListener() {
            @Override
            public void onAdLoaded(FilexAdView adView) {
                Log.i(TAG, "banner ad onAdLoaded:" + adView.getPlacementId());
            }

            @Override
            public void onAdLoadFailed(FilexAdView adView, AdError errorCode) {
                Log.i(TAG, "banner ad onAdLoaded:" + errorCode);
            }

            @Override
            public void onAdClick(FilexAdView adView) {
                Log.i(TAG, "banner ad onAdClick:" + adView.getPlacementId());
            }

            @Override
            public void onAdImpression(FilexAdView adView) {
                Log.i(TAG, "banner ad onAdImpression:" + adView.getPlacementId());
            }
        });
        mBannerAdContainer.addView(mFilexAdView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_load_native:
                loadNativeAd();
                break;
            case R.id.btn_load_banner:
                loadBannerAd();
                break;
            case R.id.btn_load_interstitial:
                loadInterstitialAd();
                break;
        }
    }

    private void loadInterstitialAd() {
        if (mFilexInterstitial == null) {
            mFilexInterstitial = new FilexInterstitial(this, INTERSTITIAL_PLACEMENT_ID, new InterstitialAdListener() {
                @Override
                public void onAdLoaded() {
                    Log.i(TAG, "Interstitial onAdLoaded");
                    mFilexInterstitial.show();
                }

                @Override
                public void onAdFailed(AdError error) {
                    Log.i(TAG, "Interstitial onAdFailed :" + error);
                }

                @Override
                public void onAdImpression() {
                    Log.i(TAG, "Interstitial onAdImpression");
                }

                @Override
                public void onAdClicked() {
                    Log.i(TAG, "Interstitial onAdClicked");
                }

                @Override
                public void onAdClosed() {
                    Log.i(TAG, "Interstitial onAdClosed");
                }
            });
        }
        mFilexInterstitial.loadAd();
    }

    private void loadBannerAd() {
        if (mFilexAdView == null) {
            initBannerAd();
        }
        mFilexAdView.loadAd();
    }

    private void loadNativeAd() {
        if (mFilexNative == null) {
            mFilexNative = new FilexNative(this, NATIVE_PLACEMENT_ID, new FilexNative.NativeAdListener() {
                @Override
                public void onNativeLoad(NativeAd nativeAd) {
                    Log.i(TAG, "native ad load success ad:" + nativeAd.toString());
                    mNativeAd = nativeAd;
                    showNativeAd();
                }

                @Override
                public void onNativeFail(AdError errorCode) {
                    Log.i(TAG, "onNative ad load failed , msg: " + errorCode.getMessage() + ",code:" + errorCode.getIntCode());
                }
            });
        }
        RequestParams requestParams = new RequestParams.Builder()
                .width(200)
                .height(400)
                .build();
        mFilexNative.loadAd(requestParams);
    }

    private void showNativeAd() {

        mNativeAdContainer.removeAllViews();
        // inflate native ad layout
        View nativeAdLayout = LayoutInflater.from(this).inflate(R.layout.layout_native_ad, null);
        TextView titleView = nativeAdLayout.findViewById(R.id.native_title);
        TextView textView = nativeAdLayout.findViewById(R.id.native_text);
        Button actionButton = nativeAdLayout.findViewById(R.id.native_cta);
        ImageView iconImageView = nativeAdLayout.findViewById(R.id.native_icon_image);
        ImageView mainImageView = nativeAdLayout.findViewById(R.id.native_main_image);
        // fill native ad data
        titleView.setText(mNativeAd.getTitle());
        textView.setText(mNativeAd.getDesc());
        actionButton.setText(mNativeAd.getCallToAction());
        mNativeAdContainer.addView(nativeAdLayout);
        // load image
        Glide.with(this).load(mNativeAd.getMainImageUrl()).centerCrop().into(iconImageView);

        Glide.with(this).load(mNativeAd.getMainImageUrl()).centerCrop().into(mainImageView);

        // register ad view for impression and click
        mNativeAd.setNativeAdEventListener(new NativeAd.NativeAdEventListener() {
            @Override
            public void onAdImpression() {
                Log.i(TAG, "onAdImpression");
            }

            @Override
            public void onAdClick() {
                Log.i(TAG, "onAdClick");
            }
        });
        mNativeAd.registerViewForAction(mNativeAdContainer);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNativeAd != null) {
            mNativeAd.unregisterViewForAction(mNativeAdContainer);
            mNativeAd = null;
        }

        if (mFilexNative != null) {
            mFilexNative.destroy();
            mFilexNative = null;
        }

        if (mFilexAdView != null) {
            mFilexAdView.destroy();
            mFilexAdView = null;
        }
    }
}
